<?php
    class Admincp_ResortController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý resort');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Resort();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            if($_POST['add']) {
                $this->_redirect('admincp/resort/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiResort($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listResort($this->_locale));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $language = new Admincp_Model_Language();
            $tag = new Admincp_Model_Tag();
            $location = new Admincp_Model_Location();
            $this->view->language = $language->listLanguageFull();
            $this->view->tag = $tag->listTagSelectbox();
            $this->view->location = $location->listLocationSelectbox(0);
            $this->view->star = $this->_model->listStar();
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $address['vi']  = trim(strip_tags($this->_arrParam['address-vi']));
                $address['zh']  = trim(strip_tags($this->_arrParam['address-zh']));
                $address['en']  = trim(strip_tags($this->_arrParam['address-en']));
                $address['trv']  = trim(strip_tags($this->_arrParam['address-trv']));
                $description['vi'] = trim($this->_arrParam['description-vi']);
                $description['zh'] = trim($this->_arrParam['description-zh']);
                $description['en'] = trim($this->_arrParam['description-en']);
                $description['trv'] = trim($this->_arrParam['description-trv']);
                $policy['vi']   = trim($this->_arrParam['policy-vi']);
                $policy['zh']   = trim($this->_arrParam['policy-zh']);
                $policy['en']   = trim($this->_arrParam['policy-en']);
                $policy['trv']   = trim($this->_arrParam['policy-trv']);
                $cozy['vi']     = trim($this->_arrParam['cozy-vi']);
                $cozy['zh']     = trim($this->_arrParam['cozy-zh']);
                $cozy['en']     = trim($this->_arrParam['cozy-en']);
                $cozy['trv']     = trim($this->_arrParam['cozy-trv']);
                $condition['vi'] = trim($this->_arrParam['condition-vi']);
                $condition['zh'] = trim($this->_arrParam['condition-zh']);
                $condition['en'] = trim($this->_arrParam['condition-en']);
                $condition['trv'] = trim($this->_arrParam['condition-trv']);
                $area_id        = $this->_arrParam['area_id'];
                $location_id    = $this->_arrParam['location_id'];
                $tag_id         = $this->_arrParam['tag_id'];
                $star           = $this->_arrParam['star'];
                $upload         = new Zend_File_Transfer_Adapter_Http();
                $info           = $upload->getFileInfo();
                $image          = $upload->getFileName('image', false);
                
                // Resize Image
                $resize = new Zend_Filter_ImageSize();
                preg_match('#\.([^\.]+)$#', $image, $matches);
                $fileExtension  = $matches[1];
                $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                $newFileName1 = $fileName . '.' . $fileExtension;
                
                $options = array('target' => BASE_PATH . '/public/files/resort/' . $newFileName1,'overwrite'=>true);
                $upload->addFilter('Rename',$options,'image');
                $upload->receive('image');
                $output = $resize->setHeight(400)
                                ->setWidth(500)
                                ->setThumnailDirectory(BASE_PATH . '/public/files/resort/')
                                ->filter(BASE_PATH . '/public/files/resort/' . $newFileName1);
                header('Content-Type: image/jpeg');
                $fh = fopen($output, 'r');
                fpassthru($fh);
                fclose($fh);
                $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
                unlink(PUBLIC_PATH . '/files/resort/' . $newFileName1);
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $data_resort = array(
                    'image' => $newFileName,
                    'location_id' => $location_id,
                    'tag_id' => $tag_id,
                    'area_id' => $area_id,
                    'star' => $star,
                    'status' => 2,
                );
                $resort_id = $this->_model->addResort($data_resort);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'resort_id' => $resort_id,
                                'language_id' => $lang['id'],
                                'name' => $value,
                                'address' => $address[$key],
                                'description' => $description[$key],
                                'policy' => $policy[$key],
                                'cozy' => $cozy[$key],
                                'condition' => $condition[$key],
                                'name_plaintext' => $strFlex->filter(strtolower($value)),
                            );
                            $this->_model->addResortLanguage($data);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/resort');
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $language = new Admincp_Model_Language();
            $location = new Admincp_Model_Location();
            $tag = new Admincp_Model_Tag();
            $this->view->data = $this->_model->getResortLanguageById($id);
            $this->view->resort = $this->_model->getResortById($id);
            $this->view->language = $language->listLanguageFull();
            $this->view->location = $location->listLocationSelectbox(0);
            $this->view->tag = $tag->listTagSelectbox();
            $this->view->star = $this->_model->listStar($this->view->resort['star']);
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $address['vi']  = trim(strip_tags($this->_arrParam['address-vi']));
                $address['zh']  = trim(strip_tags($this->_arrParam['address-zh']));
                $address['en']  = trim(strip_tags($this->_arrParam['address-en']));
                $address['trv']  = trim(strip_tags($this->_arrParam['address-trv']));
                $description['vi'] = trim($this->_arrParam['description-vi']);
                $description['zh'] = trim($this->_arrParam['description-zh']);
                $description['en'] = trim($this->_arrParam['description-en']);
                $description['trv'] = trim($this->_arrParam['description-trv']);
                $policy['vi']   = trim($this->_arrParam['policy-vi']);
                $policy['zh']   = trim($this->_arrParam['policy-zh']);
                $policy['en']   = trim($this->_arrParam['policy-en']);
                $policy['trv']   = trim($this->_arrParam['policy-trv']);
                $cozy['vi']     = trim($this->_arrParam['cozy-vi']);
                $cozy['zh']     = trim($this->_arrParam['cozy-zh']);
                $cozy['en']     = trim($this->_arrParam['cozy-en']);
                $cozy['trv']     = trim($this->_arrParam['cozy-trv']);
                $condition['vi'] = trim($this->_arrParam['condition-vi']);
                $condition['zh'] = trim($this->_arrParam['condition-zh']);
                $condition['en'] = trim($this->_arrParam['condition-en']);
                $condition['trv'] = trim($this->_arrParam['condition-trv']);
                $area_id        = $this->_arrParam['area_id'];
                $location_id    = $this->_arrParam['location_id'];
                $tag_id         = $this->_arrParam['tag_id'];
                $star           = $this->_arrParam['star'];
                $upload         = new Zend_File_Transfer_Adapter_Http();
                $info           = $upload->getFileInfo();
                $image          = $upload->getFileName('image', false);
                
                if(!empty($image)) {
                    // Resize Image
                    $resize = new Zend_Filter_ImageSize();
                    preg_match('#\.([^\.]+)$#', $image, $matches);
                    $fileExtension  = $matches[1];
                    $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                    $newFileName1 = $fileName . '.' . $fileExtension;
                    
                    $options = array('target' => BASE_PATH . '/public/files/resort/' . $newFileName1,'overwrite'=>true);
                    $upload->addFilter('Rename',$options,'image');
                    $upload->receive('image');
                    $output = $resize->setHeight(400)
                                    ->setWidth(500)
                                    ->setThumnailDirectory(BASE_PATH . '/public/files/resort/')
                                    ->filter(BASE_PATH . '/public/files/resort/' . $newFileName1);
                    header('Content-Type: image/jpeg');
                    $fh = fopen($output, 'r');
                    fpassthru($fh);
                    fclose($fh);
                    $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
                    unlink(PUBLIC_PATH . '/files/resort/' . $newFileName1);
                    unlink(PUBLIC_PATH . '/files/resort/' . $this->view->resort['image']);
                }
                
                // Update to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $data_resort = array(
                    'location_id' => $location_id,
                    'tag_id' => $tag_id,
                    'area_id' => $area_id,
                    'star' => $star,
                );
                if(!empty($image)) {
                    $data_resort['image'] = $newFileName;
                }
                $this->_model->updateResort($data_resort, $id);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'language_id' => $lang['id'],
                                'name' => $value,
                                'address' => $address[$key],
                                'description' => $description[$key],
                                'policy' => $policy[$key],
                                'cozy' => $cozy[$key],
                                'condition' => $condition[$key],
                                'name_plaintext' => $strFlex->filter(strtolower($value)),
                            );
                            $this->_model->updateResortLanguage($data, $id);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/resort');
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteResort($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'status'=>$status,
            );
            $this->_model->updateResort($data, $id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction()
        {
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
		    $name = strtolower(trim(strip_tags($this->_arrParam['name'])));
		    $status = (int) $this->_arrParam['status'];
		    $this->view->keyword = array(
	            'name'=>$name,
	            'status'=>$status,
		    );
		    $data = array(
	            'name'=>(string) '%'.str_replace(' ','%',$strFlex->filter($name)).'%',
	            'status'=>$status,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchResort($data, $this->_locale));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(40);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function loadlocationAction()
        {
            $this->_helper->layout()->disableLayout();
            $location = new Admincp_Model_Location();
            $parent = $this->_arrParam['parent'];
            if(!empty($this->_arrParam['location_id'])) {
                $this->view->location_id = $this->_arrParam['location_id'];
            }
            $this->view->location = $location->listLocationSelectbox($parent);
        }
    }
